package com.zzg.srb.common.result;

import lombok.Data;

@Data
public class R<T> {

    private Integer code;

    private String message;

    private T data;

    private R(){}

    /**
     * 返回成功结果
     * @return
     */
    public static R ok(){
        R r = new R();
        r.setCode(ResponseEnum.SUCCESS.getCode());
        r.setMessage(ResponseEnum.SUCCESS.getMessage());
        return r;
    }

    /**
     * 返回失败结果
     * @return
     */
    public static R error(){
        R r = new R();
        r.setCode(ResponseEnum.ERROR.getCode());
        r.setMessage(ResponseEnum.ERROR.getMessage());
        return r;
    }

    /**
     * 返回特定枚举
     * @param result
     * @return
     */
    public static R setResult(ResponseEnum result){
        R r = new R();
        r.setCode(result.getCode());
        r.setMessage(result.getMessage());
        return r;
    };

    /**
     * 返回数据
     * @param data
     * @return
     */
    public R<T> data(T data){
        this.setData(data);
        return this;
    }

    /**
     * 设置特殊消息
     * @param msg
     * @return
     */
    public R message(String msg){
        this.setMessage(msg);
        return this;
    }

    /**
     * 设置特殊响应码
     * @param code
     * @return
     */
    public R code(Integer code){
        this.setCode(code);
        return this;
    }
}
